package com.example.demo;

import com.example.demo.utils.Utils;
import com.example.demo.utils.WangyiUtils;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.junit.Test;
import org.openqa.selenium.Cookie;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;

import java.io.*;
import java.util.*;
import java.util.concurrent.TimeUnit;

//b站抽奖
public class BiliChouJiang {
    public static void main(String[] args) throws Exception {
        //发个400条就不发啦
        int totol = 0;
        //先获取最新列表,
        JSONObject getdatalist = Utils.getdatalist("https://api.vc.bilibili.com/topic_svr/v1/topic_svr/topic_new?topic_id=3230836");
        JSONArray cards = getdatalist.getJSONObject("data").getJSONArray("cards");
        String offsethistory = getdatalist.getJSONObject("data").getString("offset");
        //topic_new列表循环 关注,评论,转发
        for (int i = 0; i < cards.size(); i++) {
            String uname = cards.getJSONObject(i).getJSONObject("desc").getJSONObject("user_profile").getJSONObject("info").getString("uname");
            if (!cards.getJSONObject(i).getJSONObject("desc").getString("orig_dy_id").equals("0")) continue;
            String dynamic_id = cards.getJSONObject(i).getJSONObject("desc").getString("dynamic_id");
            String oid = cards.getJSONObject(i).getJSONObject("desc").getString("rid");
            String uid = cards.getJSONObject(i).getJSONObject("desc").getString("uid");
            //打印下名字,知道到哪啦
            Utils.BiliGuanzhu(uid);
            Utils.biliChoujiangQuSendmessage(oid);
            Utils.biliChoujiangQuZhuanfa(dynamic_id);
            Thread.sleep(1000 * 60);
        }
        //需写成死循环,不然容易搞到一些重复的
        //不断的获取topic_history列表,循环 关注,评论,转发
        while (true) {
            //通过offserhistory获取topic_history列表
            JSONObject getdatalisthistory = Utils.getdatalist("https://api.vc.bilibili.com/topic_svr/v1/topic_svr/topic_history?topic_name=%E4%BA%92%E5%8A%A8%E6%8A%BD%E5%A5%96&offset_dynamic_id=" + offsethistory);
            cards = getdatalisthistory.getJSONObject("data").getJSONArray("cards");
            //获取后缀
            offsethistory = getdatalisthistory.getJSONObject("data").getString("offset");
            for (int j = 0; j < cards.size(); j++) {
                //如果发超过500条,终止吧
                if (totol >= 400) System.exit(-1);
                //获取名字,知道跑到哪啦
                String uname = cards.getJSONObject(j).getJSONObject("desc").getJSONObject("user_profile").getJSONObject("info").getString("uname");
                //判断是否是转发他人的,通过orginid判断
                if (!cards.getJSONObject(j).getJSONObject("desc").getString("orig_dy_id").equals("0")) continue;
                //获取dynamic_id
                String dynamic_id = cards.getJSONObject(j).getJSONObject("desc").getString("dynamic_id");
                //获取uid
                String uid = cards.getJSONObject(j).getJSONObject("desc").getString("uid");
                //获取oid,评论需要的参数
                String oid = cards.getJSONObject(j).getJSONObject("desc").getString("rid");
                //关注
                Utils.BiliGuanzhu(uid);
                //评论
                Utils.biliChoujiangQuSendmessage(oid);
                //转发
                Utils.biliChoujiangQuZhuanfa(dynamic_id);
                System.out.println("已转发啦" + totol + "条");
                totol++;
                Thread.sleep(1000 * 60);
            }
        }

    }

    @Test
    public void test2() throws Exception {
        //先获取列表
        JSONObject getdatalist = Utils.getdatalist("https://api.vc.bilibili.com/topic_svr/v1/topic_svr/topic_new?topic_id=3230836");

        JSONArray cards = getdatalist.getJSONObject("data").getJSONArray("cards");
        String offsethistory = getdatalist.getJSONObject("data").getString("offset");
        //不断的获取topic_history列表,循环 关注,评论,转发
        for (int i = 0; i < 30; i++) {
            //通过offserhistory获取topic_history列表
            JSONObject getdatalisthistory = Utils.getdatalist("https://api.vc.bilibili.com/topic_svr/v1/topic_svr/topic_history?topic_name=%E4%BA%92%E5%8A%A8%E6%8A%BD%E5%A5%96&offset_dynamic_id=" + offsethistory);
            cards = getdatalisthistory.getJSONObject("data").getJSONArray("cards");
            //获取后缀
            offsethistory = getdatalisthistory.getJSONObject("data").getString("offset");
            for (int j = 0; j < cards.size(); j++) {
                //获取名字,知道跑到哪啦
                String uname = cards.getJSONObject(j).getJSONObject("desc").getJSONObject("user_profile").getJSONObject("info").getString("uname");
                //判断是否是转发他人的,通过orginid判断
                if (!cards.getJSONObject(j).getJSONObject("desc").getString("orig_dy_id").equals("0")) continue;
                //获取dynamic_id
                String dynamic_id = cards.getJSONObject(j).getJSONObject("desc").getString("dynamic_id");
                //获取uid
                String uid = cards.getJSONObject(j).getJSONObject("desc").getString("uid");
                //获取oid,评论需要的参数
                String oid = cards.getJSONObject(j).getJSONObject("desc").getString("rid");
                //关注
                Utils.BiliGuanzhu(uid);
                //评论
                Utils.biliChoujiangQuSendmessage(oid);
                //转发
                Utils.biliChoujiangQuZhuanfa(dynamic_id);
            }
        }
        //列表循环 关注,评论,转发

//        再获取下个列表 关注,评论,转发

    }

    //读取硬盘cookie的操作
    public static Set<Cookie> read() throws Exception {
        ObjectInputStream objectInputStream = new ObjectInputStream(new FileInputStream("D:\\cookie\\bilixiao.txt"));
        HashSet<Cookie> cookies = (HashSet<Cookie>) objectInputStream.readObject();
        return cookies;
    }

    public static WebDriver denglu() throws Exception {
        // TODO Auto-generated method stub
        System.setProperty("webdriver.chrome.driver", "D:\\ChromeDriver\\chromedriver.exe");
        ChromeOptions options = new ChromeOptions();
        Map<String, Object> prefs = new HashMap<String, Object>();
        prefs.put("download.default_directory", "d:\\");
        options.setExperimentalOption("prefs", prefs);
        ChromeDriver webDriver = new ChromeDriver(options);
        Set<Cookie> read = read();
        String url = "https://www.bilibili.com/";
        webDriver.get(url); //
        webDriver.manage().deleteAllCookies();
        for (Cookie cookie : read) {
            webDriver.manage().addCookie(cookie);
        }
        // 与浏览器同步非常重要，必须等待浏览器加载完毕
        webDriver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
        webDriver.navigate().refresh();
        return webDriver;
    }


    //开启header
    @Test
    public void headertest() throws Exception {
        String k1 = " header.put";
        String s = ":authority: api.bilibili.com\n" +
                ":method: POST\n" +
                ":path: /x/v2/reply/add\n" +
                ":scheme: https\n" +
                "accept: application/json, text/javascript, */*; q=0.01\n" +
                "accept-encoding: gzip, deflate, br\n" +
                "accept-language: zh-CN,zh;q=0.9,zh-TW;q=0.8\n" +
                "content-length: 99\n" +
                "content-type: application/x-www-form-urlencoded; charset=UTF-8\n" +
                "cookie: sid=izrhxjpb; _uuid=9B1EBC53-24DE-3C3B-5478-C7B04BE21BFA39066infoc; buvid3=41EFFC96-BFEA-4264-96FE-6C4594B8888453923infoc; LIVE_BUVID=AUTO3115929677890054; bp_video_offset_221749181=404765698027349726; bp_t_offset_221749181=404766372336171715; CURRENT_FNVAL=16; rpdid=|(um~uYJJm)m0J'ulmJ|)Jm|Y; DedeUserID=619772228; DedeUserID__ckMd5=e9b2d53e131edb2b; SESSDATA=1d509e2c%2C1609413643%2Cb3f9e*71; bili_jct=2d1cd64f8d95d254d3dc44077a7df701; CURRENT_QUALITY=0; PVID=2; bp_video_offset_619772228=409560225856138278; bp_t_offset_619772228=409560225856138278; dy_spec_agreed=1; bfe_id=1bad38f44e358ca77469025e0405c4a6\n" +
                "origin: https://t.bilibili.com\n" +
                "referer: https://t.bilibili.com/topic/name/%E8%BD%AC%E5%8F%91%E6%8A%BD%E5%A5%96/feed\n" +
                "sec-fetch-dest: empty\n" +
                "sec-fetch-mode: cors\n" +
                "sec-fetch-site: same-site\n" +
                "user-agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.116 Safari/537.36";
        Utils.getMap(k1, s);
    }

    //开启data
    @Test
    public void datatest() throws Exception {
        String k1 = "connect.data";
        String s = "oid: 409508583164977015\n" +
                "type: 17\n" +
                "message: 555\n" +
                "plat: 1\n" +
                "jsonp: jsonp\n" +
                "csrf: 2d1cd64f8d95d254d3dc44077a7df701";
        Utils.getMap(k1, s);
    }
}
